package com.focus.pay.wx.action.v2;


import com.focus.pay.wx.config.WxPayConfig;
import com.focus.pay.wx.constant.WxPayConstant;
import com.focus.pay.core.annation.PayOrderHandler;
import com.focus.pay.core.enums.ChannelTypeEnum;
import com.focus.pay.core.enums.Version;
import com.focus.pay.core.exception.PayException;
import com.focus.pay.core.pay.PayOrderInterface;
import com.focus.pay.core.utils.HttpClientUtils;
import com.focus.pay.wx.model.order.v2.WxPayModelUnified;
import com.focus.pay.wx.model.order.v2.WxPayModelUnifiedResponse;
import com.focus.pay.wx.utils.WxPayUtils;

/**
* @Author: 赵参谋
* @Date: 2021/10/13 16:03
* @Description: 微信JSAPI支付
 * <link>https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_1</link>
*/
@PayOrderHandler(channelType = ChannelTypeEnum.WxType.JSAPI,version = Version.VERSION_2)
public class WxPayOrderJSAPI extends WxPayOrder implements PayOrderInterface<Object, WxPayModelUnified, WxPayConfig> {

    @Override
    public WxPayModelUnifiedResponse payOrder(WxPayModelUnified wxPayModelUnified, WxPayConfig wxPayConfig) throws PayException {
        String res = HttpClientUtils.doPost(
                    super.buildRequestPath(WxPayConstant.WxPayPathV2.PAY_ORDER_UNIFIED),
                    super.buildRequestParam(wxPayModelUnified,wxPayConfig));
       return  this.handle(res,wxPayConfig);

    }

    /**
     * 返回数据处理
     * @param res
     * @param wxPayConfig
     * @return
     * @throws Exception
     */
    public WxPayModelUnifiedResponse handle(String res, WxPayConfig wxPayConfig) throws PayException {
        WxPayModelUnifiedResponse wxPayModelUnifiedResponse = super.resolvingResult(res, WxPayModelUnifiedResponse.class);
        wxPayModelUnifiedResponse.setPageData(WxPayUtils.JSAPIRecall(wxPayModelUnifiedResponse,wxPayConfig, Version.VERSION_2));
        return wxPayModelUnifiedResponse;
    }
}
